<template>
	<view class="example">
		<uni-forms ref="valiForm" :rules="rules" :modelValue="valiFormData" label-width="120px">
			<uni-forms-item label="班级" required name="name">
				<uni-easyinput v-model="valiFormData.name" placeholder="请输入班级" :clearable="false" disabled />
			</uni-forms-item>
			<uni-forms-item label="标题" required name="title">
				<uni-easyinput v-model="valiFormData.title" placeholder="请输入标题" />
			</uni-forms-item>
			<uni-forms-item label="活动开始时间" required>
				<uni-datetime-picker type="datetime" return-type="timestamp" v-model="valiFormData.startTime" />
			</uni-forms-item>
			<uni-forms-item label="发布" required name="isAnnounce">
				<view @click="changeStatus"
					style="width: 100%;box-sizing: border-box;border-radius: 4px;border: 1px solid #e5e5e5;display: flex;">
					<picker style="padding: 10px; width: 100%;" @change="bindPickerChange" :value="index"
						:range="columns">
						<view class="uni-input">{{ columns[index] }}</view>
					</picker>
				</view>
			</uni-forms-item>
			<uni-forms-item label="内容" required name="intro">
				<textarea v-model="valiFormData.intro" maxlength="999999"
					style="width:100% !important;min-height:400px !important;font-size: 14px !important;"
					class="textarea" type="textarea" placeholder="请输入活动内容" />
			</uni-forms-item>
		</uni-forms>
		<button type="primary" @click="submit('valiForm')" v-show="openSubmit">提交</button>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		listInfo,
		getInfo,
		addInfo,
		updateInfo,
		delInfo
	} from '@/api/jz/classActivity/index.js';
	import {

		getInfoId,

	} from '@/utils/auth';

	export default {
		data() {
			return {
				openSubmit: true,
				index: 0,
				columns: ['否', '是'],
				show2: false,
				// 校验表单数据
				valiFormData: {
					name: '',
					title: '',
					intro: '',
					isAnnounce: '',
					startTime: ""
				},
				// 校验规则
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '班级不能为空'
						}]
					},
					title: {
						rules: [{
							required: true,
							errorMessage: '标题不能为空'
						}]
					}
				},
				classInfo: {},
				type: 'textarea',
				border: true,
				show: false,
				startTime: '',
				form: {
					title: '',
					intro: '',
				},
				jsData: {},
				params: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: true
				},
				sendStatus: 0,
			};
		},
		mounted() {},
		methods: {
			bindPickerChange(e) {
				this.index = e.detail.value
			},
			changeStatus() {
				this.show2 = true;
			},
			selectTime() {
				this.show = true;
			},
			confirm(e) {
				this.input = '';
				if (this.params.year) this.input += e.year;
				if (this.params.month) this.input += '-' + e.month;
				if (this.params.day) this.input += '-' + e.day;
				if (this.params.hour) this.input += ' ' + e.hour;
				if (this.params.minute) this.input += ':' + e.minute;
				if (this.params.second) this.input += ':' + e.second;
				this.valiFormData.startTime = this.input;
			},
			showToast() {
				this.$refs.uToast.show({
					title: '操作成功',
					type: 'success',
				})

				setTimeout(function() {
					uni.navigateBack({
						delta: 1
					});
				}, 2000)

			},
			showErro() {
				this.$refs.uToast.show({
					title: '操作失败',
					type: 'error',
				})
			},
			submit(ref) {
				this.$refs[ref].validate().then(res => {
					this.openSubmit = false;
					if (res) {
						let classActivity = {
							"classId": this.classInfo.id,
							"classCode": this.classInfo.code,
							"title": this.valiFormData.title,
							"content": this.valiFormData.intro,
							"isAnnounce": this.index,
							"startTime": this.valiFormData.startTime,
							"teacherId": getInfoId(),
						}

						addInfo(classActivity).then((res) => {
							if (res.code == 200) {
								this.showToast();
							} else {
								this.showErro();
							}
						})
					} else {

					}
				})
			}
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			// this.$refs.uForm.setRules(this.rules);
		},
		onLoad(data) {
			this.classInfo = JSON.parse(decodeURIComponent(data.data))
			this.valiFormData.name = this.classInfo.className;
			this.jsData = JSON.parse(decodeURIComponent(data.data))
		}
	}
</script>

<style>
	.textarea {
		display: flex;
		box-sizing: border-box;
		flex-direction: row;
		align-items: center;
		border: 1px solid #dcdfe6;
		// border-radius: 4px;
	}

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
</style>